﻿/*
 * 
Copyright (C) 2008 ForceMem (Siarhei Fedarenko)
GNU General Public License version 3 (GPLv3)
 * 
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TranscriptionWriter
{
    public partial class KeyBoardPhonetic : UserControl
    {
        Button[] shiftedButtons = new Button[] { };
        Button[] buttons = new Button[] { };
        char[] buttonKeys = new char[] {};
        
        public KeyBoardPhonetic()
        {
            InitializeComponent();

            this.shiftedButtons = new Button[] { 
                btW, btE, btA, btS, btD, btZ,
                btR, btT, btF, btG, btH, btV, btB, btN, 
                btM, btU, btI, btO, btP, btJ, btK, btL, btSpace, button1,
                btQ, btX, btC, btY
            };
            this.buttons = new Button[] { 
                btW, btE, btA, btS, btD, btZ,
                btR, btT, btF, btG, btH, btV, btB, btN, 
                btM, btU, btI, btO, btP, btJ, btK, btL, btSpace, button1,
                btQ, btX, btC, btY,            
            };

            this.buttonKeys = new char[] { 
                'W', 'E', 'A', 'S', 'D', 'Z',
                'R', 'T', 'F', 'G', 'H', 'V', 'B', 'N', 
                'M', 'U', 'I', 'O', 'P', 'J', 'K', 'L', ' ', ':',
                'Q', 'X', 'C', 'Y',
            };

            foreach (Button bt in buttons)
            {
                bt.Tag = bt.Text;
            }
            IsShiftUp(this.m_IsShiftDown);
            SetStyle();
        }

        void SetStyle()
        {
            List<Button> btns = new List<Button>();
            btns.Add(this.btShift);
            btns.Add(this.button2);
            btns.Add(this.button3);
            btns.Add(this.button4);
            if (Program.IsNiceOSForSimpleButton)
            {
                foreach (Button bt in buttons)
                {
                    bt.FlatStyle = FlatStyle.Standard;
                    bt.UseVisualStyleBackColor = true;
                }
                foreach (Button bt in btns)
                    bt.FlatStyle = FlatStyle.Standard;
            }
        }

        public string GetChatByKey(char key)
        {
            if (key == ' ')
            {
                return " ";
            }
            if (Array.IndexOf(buttonKeys, char.ToUpper(key)) != -1)
            {
                Button bt = buttons[Array.IndexOf(buttonKeys, char.ToUpper(key))];
                return bt.Text;
            }
            else return null;

        }

        public TextBox m_TextBox;
        public TextBox TextBox
        {
            get
            {
                return m_TextBox;
            }
            set
            {
                m_TextBox = value;
            }
        }

        private void bt9_Click(object sender, EventArgs e)
        {
            if (this.TextBox == null) return;
            string val = ((Button)sender).Text;
            if (sender == this.btSpace )
                val = " ";
            MainForm.InsertText(val, this.TextBox);
        }

        bool m_IsShiftDown = false;

        public void SwitchShift()
        {
            this.btShift_Click(null, EventArgs.Empty);
        }

        private void btShift_Click(object sender, EventArgs e)
        {
            m_IsShiftDown = !m_IsShiftDown;
            IsShiftUp(m_IsShiftDown);
            if( m_IsShiftDown )
                this.btShift.Font = new Font(this.btShift.Font.Name, 15, FontStyle.Bold | FontStyle.Underline);
            else
                this.btShift.Font = new Font(this.btShift.Font.Name, 13, FontStyle.Regular);
            this.TextBox.Select();
        }

        private void IsShiftUp(bool isShiftDown)
        {
            foreach (Button bt in shiftedButtons)
            {
                string s = ((string)bt.Tag).ToLower();
                if (isShiftDown)
                {
                    if (s.IndexOf(' ') != -1)
                        bt.Text = s.Split(' ')[1];
                    else if (s.Length == 2)
                        bt.Text = s[1].ToString();
                    else if (s.Length == 3) // for ':'
                        bt.Text = s.Substring(2, 1);
                }
                else
                {
                    if (s.IndexOf(' ') != -1)
                        bt.Text = s.Split(' ')[0];
                    else if (s.Length == 2)
                        bt.Text = s[0].ToString();
                    else if (s.Length == 3)
                        bt.Text = s.Substring(0, 2);
                }
            }
        }

        private void btQ_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == (Keys.ShiftKey | Keys.Shift))
            {
                this.SwitchShift();
            }
        }

        private void btBackSpace_Click(object sender, EventArgs e)
        {
            if (this.TextBox == null) return;
            if (this.TextBox.SelectionStart ==0) return;
            if (!string.IsNullOrEmpty(this.TextBox.Text))
            {
                int start = this.TextBox.SelectionStart;
                if (this.TextBox.SelectionLength == 0)
                {
                    if (start - 1 >= 0)
                        this.TextBox.Text = this.TextBox.Text.Remove(start - 1, 1);
                    --start;
                }
                else
                {
                    this.TextBox.Text = this.TextBox.Text.Remove(start, this.TextBox.SelectionLength);
                }
                // Not clearly why all text here is selected?
                // this.TextBox.Select();
                // Therefore it is necessary to do
                this.TextBox.Focus();
                if (start >= 0)
                {
                    this.TextBox.SelectionStart = start;
                    this.TextBox.SelectionLength = 0;
                }
            }
        }
    }
}